package com.chy.dao.impl;

import com.chy.bean.Gallery;
import com.chy.bean.Order;
import com.chy.dao.OrderDao;
import com.chy.jdbcutil.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository
public class OrderDaoImpl extends JDBCUtil implements OrderDao {
    static {
        try {
            JDBCUtil.testConnection();
            System.out.println(conn + "===1312312312");
            PreparedStatement pstm = conn.prepareStatement("CREATE TABLE user (id int(32) NOT NULL , username varchar(32) DEFAULT NULL,password varchar(32) DEFAULT NULL,PRIMARY KEY (id))");
            pstm.executeUpdate();
            PreparedStatement pstm2 = conn.prepareStatement("INSERT INTO user (id,username,password) values (1,'123','456'),(2,'wan','123'),(3,'wang','1234'),(4,'1234','4567'),(5,'ljw','12138')");
            pstm2.executeUpdate();
            PreparedStatement pstm3 = conn.prepareStatement("CREATE TABLE TheOrder (id int(32) NOT NULL ,code varchar(32) DEFAULT NULL,money Double(32) DEFAULT NULL,picture varchar(32) DEFAULT NULL,PRIMARY KEY (id))");
            pstm3.executeUpdate();
            PreparedStatement pstm4 = conn.prepareStatement("INSERT INTO TheOrder (id,code,money,picture) values (1,'1931',15.1,'头像01.jpg'),(2,'1931',465.1,'头像02.jpg'),(3,'1999',54.3,'头像03.jpg')");
            pstm4.executeUpdate();
            PreparedStatement pstm5 = conn.prepareStatement("CREATE TABLE OrderGallery (id int(32) NOT NULL ,imgname varchar(32) DEFAULT NULL,PRIMARY KEY (id))");
            pstm5.executeUpdate();
            PreparedStatement pstm6 = conn.prepareStatement("INSERT INTO OrderGallery (id,imgname) values (1,'头像01.jpg'),(2,'头像02.jpg'),(3,'头像03.jpg')");
            pstm6.executeUpdate();
        } catch (ClassNotFoundException | IOException | SQLException e) {
            System.out.println(e);
        }
    }
    public static Integer initId = 4; //记录id
    @Override
    public List<Order> getAll() {
        ArrayList<Order> listAll = new ArrayList<>();
        try {
            JDBCUtil.testConnection();
            System.out.println("==============================================");
            PreparedStatement pst = conn.prepareStatement("SELECT * FROM TheOrder");
            ResultSet resultSet = pst.executeQuery();
            while (resultSet.next()){
                Order order = new Order();
                order.setId(resultSet.getInt(1));
                order.setCode(resultSet.getString(2));
                order.setMoney(resultSet.getDouble(3));
                order.setPicture("http://localhost:8080/static/"+resultSet.getString(4));
                listAll.add(order);
                System.out.println(listAll);
            }
        } catch (ClassNotFoundException | IOException | SQLException e) {
            System.out.println("e = " + e);
        }
        return listAll;
    }

    @Override
    public void delete(Order order) {
        try {
            JDBCUtil.testConnection();
            PreparedStatement pst = conn.prepareStatement("delete from TheOrder where id = ?");
            pst.setInt(1, order.getId());
            int flag = pst.executeUpdate();
            System.out.println(flag+"detele里的结果输出！！！====1111等于true");
        } catch (ClassNotFoundException | IOException | SQLException e) {
            System.out.println("e = " + e);
        }
    }

    @Override
    public void add(Order order,String addImgId) {
        try {
            if (order.getId() == null){
                order.setId(initId++);
            }
            JDBCUtil.testConnection();
            PreparedStatement pst = conn.prepareStatement("INSERT INTO TheOrder (id,code,money,picture)values (?,?,?,?)");
            pst.setInt(1,order.getId());
            pst.setString(2,order.getCode());
            pst.setDouble(3,order.getMoney());
            pst.setString(4,addImgId);
            boolean rest = pst.execute();
            System.out.println("rest = " + rest);
        } catch (ClassNotFoundException | IOException | SQLException e) {
            System.out.println("e = " + e);
        }
    }

    @Override
    public void update(Order order,String updateImgId) {
        try {
            JDBCUtil.testConnection();
            PreparedStatement pst = conn.prepareStatement("UPDATE TheOrder SET code= ? ,money= ? ,picture= ? WHERE id= ? ");
            pst.setString(1,order.getCode());
            pst.setDouble(2,order.getMoney());
            pst.setString(3,updateImgId);
            pst.setInt(4,order.getId());
            int i = pst.executeUpdate();
            System.out.println("i = " + i);
        } catch (ClassNotFoundException | IOException | SQLException e) {
            System.out.println("e = " + e);
        }
    }

    @Override
    public void uploading(String name) {
        try {
            JDBCUtil.testConnection();
            PreparedStatement pst = conn.prepareStatement("INSERT INTO OrderGallery (id,imgname)values (?,?)");
            pst.setInt(1,initId++);
            pst.setString(2,name);
            boolean rest = pst.execute();
            System.out.println("rest = " + rest);
        } catch (ClassNotFoundException | IOException | SQLException e) {
            System.out.println("e = " + e);
        }
    }

    @Override
    public List<Gallery> getAllImg() {
        ArrayList<Gallery> allListImg = new ArrayList<>();
        try {
            JDBCUtil.testConnection();
            System.out.println("==============================================");
            PreparedStatement pst = conn.prepareStatement("SELECT * FROM OrderGallery");
            ResultSet resultSet = pst.executeQuery();
            while (resultSet.next()){
                Gallery gallery = new Gallery();
                gallery.setId(resultSet.getInt(1));
                gallery.setImgname(resultSet.getString(2));
                allListImg.add(gallery);
                System.out.println(allListImg);
            }
        } catch (ClassNotFoundException | IOException | SQLException e) {
            System.out.println("e = " + e);
        }
        return allListImg;
    }

}
